package common;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.wxaapp.WxaConfig;
import com.jfinal.wxaapp.WxaConfigKit;

import common.inter.CorsHandler;
import common.inter.GlobalExceptionInterceptor;
import common.kit.PropKits;
import common.route.AdminRoutes;
import common.route.ApiRoutes;
import common.task.MainTask;
import controller.admin.QiniuController;
import model._MappingKit;

public class MainConfig extends JFinalConfig {
    public static void main(String[] args) {
        UndertowServer.create(MainConfig.class, "undertow.properties").start();
    }

    @Override
    public void configConstant(Constants constants) {
        PropKit.useFirstFound("config_pro.properties", "main_config_dev.properties");
        constants.setDevMode(PropKits.isDevMode());
        //微信开发输出请求交互的 xml 与 json 数据
        ApiConfigKit.setDevMode(PropKits.isDevMode());
        constants.setInjectDependency(true);
        constants.setMaxPostSize(10485760);
        constants.setError404View("/404.html");
        constants.setError500View("/500.html");
        constants.setError401View("/404.html");
        constants.setError403View("/404.html");
    }

    @Override
    public void configRoute(Routes routes) {
    	routes.add(new AdminRoutes());
    	routes.add(new ApiRoutes());
        routes.add("/qiniu" , QiniuController.class);
    }

    public void configEngine(Engine engine) {
        engine.setDevMode(PropKits.isDevMode());
        //添加后台权限类供模板引擎使用
        engine.addSharedMethod(new StrKit());
    }
    
    public static HikariCpPlugin createHikariCpPlugin() {
		return new HikariCpPlugin(PropKits.getJdbcUrl(), PropKits.getJdbcUser(), PropKits.getJdbcPsw());
	}

    public void configPlugin(Plugins plugins) {
        //添加连接池
        HikariCpPlugin dbPlugin = MainConfig.createHikariCpPlugin();
        dbPlugin.setConnectionInitSql("set names utf8mb4;"); //支持emoji表情
        plugins.add(dbPlugin);
        //添加ARP组件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dbPlugin);
        arp.setShowSql(PropKits.isDevMode());
        arp.addSqlTemplate("common.sql");
        arp.setDialect(new MysqlDialect());
        dbPlugin.setDriverClass("com.mysql.jdbc.Driver");
        _MappingKit.mapping(arp);
        plugins.add(arp);
        //添加EhCache缓存
        plugins.add(new EhCachePlugin());
        //添加定时任务组件
        plugins.add(new MainTask().startTasks());
    }

    public void configInterceptor(Interceptors interceptors) {
        interceptors.addGlobalActionInterceptor(new GlobalExceptionInterceptor());
    }

    public void configHandler(Handlers handlers) {
    	handlers.add(new CorsHandler());
    }

    @Override
    public void onStart() {
    	WxaConfig wxaConfig = new WxaConfig();
    	wxaConfig.setAppId(PropKits.getSmallAppId());
    	wxaConfig.setAppSecret(PropKits.getSmallAppSecret());
        WxaConfigKit.putWxaConfig(wxaConfig);
    }
}
